package com.kzx.blog.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.RequestParameterBuilder;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.List;

/**
 * @author kongz
 * @version 1.0 2021/11/23 10:34
 * @description swagger3配置类
 */
@Configuration
@EnableOpenApi
public class SwaggerConfig {

    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.kzx.blog.controller"))
                .build()
                .globalRequestParameters(getHeaderParam());
    }

    private ApiInfo apiInfo() {
        return new ApiInfo(
                "kzx的swaggerAPI文档",
                "卑微码农",
                "1.0",
                "urn:tos",
                new Contact(
                        "孔子雄",
                        "",
                        "2603740065@qq.com"),//作者信息
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList<>());
    }

    /**
     * 设置报文头部信息
     */
    private List<RequestParameter> getHeaderParam() {
        RequestParameterBuilder builder = new RequestParameterBuilder();
        List<RequestParameter> list = new ArrayList<>();

        builder.name("access_token").description("token令牌").in(ParameterType.HEADER).required(Boolean.FALSE).build();
        list.add(builder.build());

        return list;
    }
}
